home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BGETBF.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  142 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bgetbf.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* local headers */
  18. #include "blkio_.h"
  19.  
  20. /*man---------------------------------------------------------------------------
  21. NAME
  22.      bgetbf - get a block field from a block file
  23.  
  24. SYNOPSIS
  25.      #include <blkio.h>
  26.  
  27.      int bgetbf(bp, bn, offset, buf, bufsize)
  28.      BLKFILE *bp;
  29.      bpos_t bn;
  30.      size_t offset;
  31.      void *buf;
  32.      size_t bufsize;
  33.  
  34. DESCRIPTION
  35.      The bgetbf function reads a field from block number bn in the
  36.      block file associated with BLKFILE pointer bp into buf.  The
  37.      field begins offset characters from the beginning of the block
  38.      and is bufsize characters long.  buf must point to a storage area
  39.      at least bufsize characters long.  Block numbering starts at 1.
  40.  
  41.      bgetbf will fail if one or more of the following is true:
  42.  
  43.      [EINVAL]       bp is not a valid BLKFILE pointer.
  44.      [EINVAL]       bn or bufsize is less than 1.
  45.      [EINVAL]       buf is the NULL pointer.
  46.      [BEBOUND]      offset + bufsize extends beyond the
  47.                     boundary of block bn.
  48.      [BEEOF]        There are not bn blocks in the file.
  49.      [BEEOF]        End of file encountered within block bn.
  50.      [BENOPEN]      bp is not open for reading.
  51.  
  52. SEE ALSO
  53.      bgetb, bgethf, bputbf.
  54.  
  55. DIAGNOSTICS
  56.      Upon successful completion, a value of 0 is returned.  Otherwise,
  57.      a value of -1 is returned, and errno set to indicate the error.
  58.  
  59. ------------------------------------------------------------------------------*/
  60. #ifdef AC_PROTO
  61. int bgetbf(BLKFILE *bp, bpos_t bn, size_t offset, void *buf, size_t bufsize)
  62. #else
  63. int bgetbf(bp, bn, offset, buf, bufsize)
  64. BLKFILE *bp;
  65. bpos_t bn;
  66. size_t offset;
  67. void *buf;
  68. size_t bufsize;
  69. #endif
  70. {
  71.     int    i    = 0;
  72.     size_t    bufno    = 0;
  73.  
  74.     /* validate arguments */
  75.     if (!b_valid(bp) || bn < 1 || buf == NULL || bufsize < 1) {
  76.         errno = EINVAL;
  77.         return -1;
  78.     }
  79.  
  80.     /* check if not open for reading */
  81.     if (!(bp->flags & BIOREAD)) {
  82.         errno = BENOPEN;
  83.         return -1;
  84.     }
  85.  
  86.     /* check if block boundary is crossed */
  87.     if ((offset + bufsize) > bp->blksize) {
  88.         errno = BEBOUND;
  89.         return -1;
  90.     }
  91.  
  92.     /* check if not bn blocks in file */
  93.     if (bn >= bp->endblk) {
  94.         errno = BEEOF;
  95.         return -1;
  96.     }
  97.  
  98.     /* check if not buffered */
  99.     if (bp->bufcnt == 0) {
  100.         if (b_ugetf(bp, bn, offset, buf, bufsize) == -1) {
  101.             BEPRINT;
  102.             return -1;
  103.         }
  104.         return 0;
  105.     }
  106.  
  107.     /* search buffer list for block */
  108.     for (i = 1; i <= bp->bufcnt; ++i) {
  109.         if ((b_blockp(bp, (size_t)i)->bn == bn)
  110.                    && (b_blockp(bp, (size_t)i)->flags & BLKREAD)) {
  111.             bufno = i;
  112.             break;
  113.         }
  114.     }
  115.  
  116.     /* if not found, use least recently used buffer */
  117.     if (bufno == 0) {
  118.         bufno = bp->least;
  119.         if (b_put(bp, bufno) == -1) {    /* flush previous contents */
  120.             BEPRINT;
  121.             return -1;
  122.         }
  123.         b_blockp(bp, bufno)->flags = 0;
  124.         b_blockp(bp, bufno)->bn = bn;
  125.         if (b_get(bp, bufno) == -1) {    /* read block from file */
  126.             BEPRINT;
  127.             return -1;
  128.         }
  129.     }
  130.  
  131.     /* copy from block buffer into buf */
  132.     memcpy(buf, ((char *)b_blkbuf(bp, bufno) + offset), bufsize);
  133.  
  134.     /* move block buffer bufno to most recently used end of list */
  135.     if (b_mkmru(bp, bufno) == -1) {
  136.         BEPRINT;
  137.         return -1;
  138.     }
  139.  
  140.     return 0;
  141. }
  142.